package com.tqz.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.tqz.dao.NewsSerch_user;

@WebServlet("/DoLoginServlet")
public class DoLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public DoLoginServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");// 获取登陆信息
		String password = request.getParameter("password");
		// 防止中文乱码，设置字符集
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charSet=utf-8");
		String repath = request.getContextPath();// 获取当前路径上下文
		HttpSession session = request.getSession();
		NewsSerch_user user=new NewsSerch_user();
		boolean checkuser=user.checkUser(username, password);
		System.out.println(checkuser);
		if (checkuser) {
			// 登陆成功服务器内部转发
			session.setAttribute("username", username);// 在session里面保存一个用户登陆状态
			request.getRequestDispatcher("/loginsuccess.jsp").forward(request, response);// Dispatcher：分配器,内部转发不能加上下文
		} else {
			// 登陆失败则重定向
			response.sendRedirect(repath + "/loginfailed.jsp");
		}
	}
}
